<template>
  <div>
    <tablePaging :operationItem="operationItem" :tableItem="tableItem"/>
    <fileImport v-model:importVisible="importVisible" :importItem="importItem" @onError="onError"
                @onSuccess="onSuccess"/>
  </div>
</template>
<script setup>
import tablePaging from '@/views/common/component/table-paging'
import fileImport from '@/views/common/component/fileImport'
import {onMounted, reactive, ref} from 'vue'
import {operationItem, tableItem} from './const.js'
import {ElMessage} from 'element-plus'
import {useRouter} from 'vue-router'
import {listDictItemByCode} from '@/api/config/dic'
import {delBank, publishBank} from '@/api/resource/bank'

const {proxy} = getCurrentInstance();
const router = useRouter()
const importVisible = ref(false)
const importItem = reactive({
  downUrl: '/admin/resource/question/bank/download',//文件下载地址
  upUrl: '/admin/resource/question/bank/import',//文件上传地址
  blob: true,//二进制文件下载
  fileDown() {
  },//自定义文件下载方法 如果传入下载地址会失效 如果都不传会返回一个fileDown函数 自己@fileDown接收
  downTitle: '',//下载标题 默认1、模板下载
  downButtonText: '',//下载按钮 下载
  upButtonText: '',//上传按钮 上传
  upTitle: '',//上传标题 默认2、表格上传
  downName: '题库模版'
})
const onSuccess = (file) => {
  tableItem.pageInfo.paging()
}
const onError = (file) => {
}
const addMoresCourse = () => {
  console.log(1111111111111111)
  router.push({
    path: '/modules/resource/bank/addMore',
    query: {type: 'add'}
  })
}

function importHandle() {
  importVisible.value = true
}

function exportHandle() {
  proxy.download("/admin/resource/question/bank/export", tableItem.pageInfo.request, `题库列表.xlsx`);
}

function initTableItem() {
  let menuArray = operationItem.menuArray
  let addMenu = menuArray.find(item => item.code === 'add')
  addMenu.handle = goAdd

  let delMenu = menuArray.find(item => item.code === 'del')
  delMenu.handle = delBatch

  // let importMenu = menuArray.find(item => item.code === 'import')
  // importMenu.handle = importHandle

  let exportMenu = menuArray.find(item => item.code === 'export')
  exportMenu.handle = exportHandle


  let btnArray = tableItem.btnArray
  let detailBtn = btnArray.find(item => item.code === 'detail')
  detailBtn.handle = goDetail

  let editBtn = btnArray.find(item => item.code === 'edit')
  editBtn.handle = goEdit

  let previewBtn = btnArray.find(item => item.code === 'preview')
  previewBtn.handle = preview

  let publishedBtn = btnArray.find(item => item.code === 'published')
  publishedBtn.handle = (row) => {
    publish(row, true)
  }
  let unpublishedBtn = btnArray.find(item => item.code === 'unpublished')
  unpublishedBtn.handle = (row) => {
    publish(row, false)
  }
  let addMore = menuArray.find(item => item.text === '批量添加')
  addMore.handle = addMoresCourse
}

function goDetail(row) {
  router.push({
    path: '/modules/resource/bank/detail',
    query: {id: row.id}
  })
}

function goAdd() {
  router.push({
    path: '/modules/resource/bank/save',
    query: {type: 'add'}
  })
}

function goEdit(row) {
  router.push({
    path: '/modules/resource/bank/save',
    query: {id: row.id, type: 'edit'}
  })
}

function preview(row) {
  let bankUrl
  window.open(bankUrl, '_bank')
}

const selectedRow = ref([])
tableItem.selectChange = (row) => {
  //构建需要删除的id列表
  selectedRow.value = row.map((item) => {
    return item.id
  })
}

//批量删除
function delBatch() {
  if (selectedRow.value.length < 1) {
    return ElMessage.warning('请先选择题库');
  }
  proxy.$modal.confirm('确认要删除选中的题库吗?').then(function () {
    return delBank(selectedRow.value);
  }).then(res => {
    tableItem.pageInfo.paging()
    ElMessage.success(res.msg);
  }).catch(() => {
  });
}

//上下架
function publish(row, isPublish) {
  let requestData = {id: row.id}
  requestData.publishedFlag = isPublish ? 1 : 0
  publishBank(requestData).then((res) => {
    if (res.code === 200) {
      tableItem.pageInfo.paging()
      ElMessage.success('操作成功');
    }
  })
}

const initOptions = () => {
  let searchItemArray = operationItem.SearchItem
  let courseTypeItem = searchItemArray.find(item => item.code == 'courseType')
  let bankTypeItem = searchItemArray.find(item => item.code == 'type')

  listDictItemByCode({typeCode: 'courseTypeCode'}).then(res => {
    courseTypeItem.options = res.rows
  })
  listDictItemByCode({typeCode: 'bankTypeCode'}).then(res => {
    bankTypeItem.options = []
    const bankTypeList = res.rows
    bankTypeList.map(item => {
      bankTypeItem.options.push({label: item.name, value: item.id})
    })
  })
}

onMounted(() => {
  initTableItem()
  initOptions()
})
</script>

<style></style>
